package com.glory.dgcpcpda.activity.cpc;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;


import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSONObject;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.adapter.CpcLotInfoAdapter;
import com.glory.dgcpcpda.base.BaseActivity;
import com.glory.dgcpcpda.entity.AlarmInfo;
import com.glory.dgcpcpda.entity.CpcLotInfo;
import com.glory.dgcpcpda.entity.LotInfoEntity;
import com.glory.dgcpcpda.entity.response.BaseResponseBean;
import com.glory.dgcpcpda.utils.LotUtils;
import com.glory.dgcpcpda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.apache.commons.lang3.StringUtils;
import org.json.JSONException;


import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class CpcLotInfoActivity extends BaseActivity {
    @BindView(R.id.etlotId)
    FreeUI_ClearEditText etlotId;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.tvOrderNum)
    TextView tvOrderNum;
    @BindView(R.id.tvsxNum)
    TextView tvsxNum;
    @BindView(R.id.rvLotInfoList)
    RecyclerView rvLotInfoList;
    private CpcLotInfo info;
    private CpcLotInfoAdapter lotAdapter;
    Handler handler;

    class MyHandler extends Handler{
        @Override
        public void handleMessage(Message msg) {
            //System.out.println("#########:"+msg);

            CpcLotInfo  info2= (CpcLotInfo) msg.obj;

            if(info2.getINFO()!=null){
                 tvOrderNum.setText(info.getINFO().getOrderNum());
                 tvsxNum.setText(info.getINFO().getSxNum());
            }else{
                tvOrderNum.setText("");
                tvsxNum.setText("");
            }
            List<CpcLotInfo> infoList=info2.getList();

            lotAdapter.setNewData(infoList);

        }
    }
    @Override
    protected int getLayoutResId() {
        return R.layout.activity_lot_info;
    }

    @Override
    protected void initView() {
        setMainLayout(llMain);
        tvTitle.setText("预结批查询");
        tvBaseSubmit.setText("查询");
        handler = new MyHandler();
        initRecyclerView(rvLotInfoList,this);
        lotAdapter=new CpcLotInfoAdapter(null);
        lotAdapter.isFirstOnly(true);
        lotAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        rvLotInfoList.setAdapter(lotAdapter);
        initEdit2(etlotId,s -> initData());


    }

    @Override
    protected void initData() {
        if(StringUtils.isNotEmpty(getText(etlotId))){
            getData();
        }
    }

    public void getData(){
        LotUtils.searchLotInfo(CpcLotInfoActivity.this, getText(etlotId), getSupportFragmentManager(), new LotUtils.netCallback<LotInfoEntity>() {
            @Override
            public void onSuccess(LotInfoEntity response) {
                new Thread() {//网络请求需要在子线程中完成
                    @Override
                    public void run() {
                        //System.out.println("返回结果："+res);

                        String data = "lotId="+getText(etlotId);//POST请求的参数
                        //String res = post("http://192.168.60.223:8080/auto_sync_date/jp", data);//调用我们写的post方法
                        String res = post("http://192.168.1.18:8080/auto_sync_date/jp", data);//调用我们写的post方法
                        System.out.println("返回结果："+res.length());
                        if(res.length()>2){
                            info= JSONObject.parseObject(res,CpcLotInfo.class);
                            // tvOrderNum.setText(info.getINFO().getOrderNum());
                            //tvsxNum.setText(info.getINFO().getSxNum());
                            Message msg = new Message();
                            msg.obj = info;
                            handler.sendMessage(msg);

                        }else{

                            System.out.println("暂无数据");
                        }



                    }
                }.start();

            }

            @Override
            public void onFail(Throwable t) {
                System.out.println("返回失败！");
            }

        });

    }
    public String post(String url1, String data) {

        try {
            URL url = new URL(url1);
            HttpURLConnection Connection = (HttpURLConnection) url.openConnection();//创建连接
            Connection.setRequestMethod("POST");
            Connection.setConnectTimeout(3000);
            Connection.setReadTimeout(3000);
            Connection.setDoInput(true);
            Connection.setDoOutput(true);
            Connection.setUseCaches(false);
            Connection.connect();
            DataOutputStream dos = new DataOutputStream(Connection.getOutputStream());
            String title = data;//这里是POST请求需要的参数字符串类型，例如"id=1&data=2"
            dos.write(title.getBytes());
            dos.flush();
            dos.close();//写完记得关闭
            int responseCode = Connection.getResponseCode();
            if (responseCode == Connection.HTTP_OK) {//判断请求是否成功
                InputStream inputStream = Connection.getInputStream();
                ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
                byte[] bytes = new byte[1024];
                int length = 0;
                while ((length = inputStream.read(bytes)) != -1) {
                    arrayOutputStream.write(bytes, 0, length);
                    arrayOutputStream.flush();
                }//读取响应体的内容
                String s = arrayOutputStream.toString();

                return s;//返回请求到的内容，字符串形式
            } else {
                return "-1";//如果请求失败返回-1
            }
        } catch (Exception e) {
            return "-1";//出现异常也返回-1
        }
    }
    @OnClick({R.id.tvBaseSubmit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvBaseSubmit:
                initData();
                break;
        }
    }
}
